<template>
  <div id="container">

    <div id="header">
      <div class="title">
        <div class="h1">环保公众监督系统</div>
      </div>
    </div>
    <div id="body">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <h1 class="info">账户信息</h1>
          </div>
        </template>
        <div class="registerwin">
          <!-- 头像上传 -->
          <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="120px" :size="formSize" status-icon>
            <div class="registerform">
              <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username" class="input-animated" placeholder="请输入用户名" />
              </el-form-item>
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" class="input-animated" placeholder="请输入真实姓名" />
              </el-form-item>
              <el-form-item label="手机号码" prop="telephone">
                <el-input v-model="form.telephone" class="input-animated" placeholder="请输入11位手机号" />
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="form.password" placeholder="请输入密码" class="input-animated" />
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="form.checkPass" placeholder="确认密码" class="input-animated" />
              </el-form-item>
              <el-form-item>
                <div class="btn-container">
                <el-button type="primary" @click="onSubmit(ruleFormRef)">注册</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                <el-button @click="toLogin" class="button-animated" type="primary">登录</el-button>
                </div>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </el-card>
    </div>
    <div class="footer">
      <div class="bubbles">
        <div v-for="i in 128" :key="i" class="bubble" :style="getBubbleStyle()"></div>
      </div>
      <div class="content">
        <div>
          <div>
            <b>关于东软</b>
            <a href="https://www.neuedu.com/page/Company-Profile">公司简介</a>
            <a href="https://www.neuedu.com/channel/MT.html">管理团队</a>
            <a href="https://www.neuedu.com/page/Business-philosophy">文化理念</a>
            <a href="https://www.neuedu.com/channel/history.html">发展历程</a>
          </div>
          <div>
            <b>绿色和平</b>
            <a href="https://www.greenpeace.org.cn/climate-and-energy/">气候与能源</a>
            <a href="https://www.greenpeace.org.cn/biodiversity/">保护生物多样性</a>
            <a href="https://www.greenpeace.org.cn/live-sustainably/">可持续生活</a>
          </div>
        </div>
        <div>
          <div>
            <b>投资者</b>
            <a href="#">公司治理</a>
            <a href="#">股本变化</a>
            <a href="#">公司公告</a>
            <a href="#">财务报告</a>
          </div>
          <div>
            <b>联系我们</b>
            <a href="#">业务咨询</a>
            <a href="#">售后咨询</a>
            <a href="#">致电咨询</a>
            <a href="#">其他咨询</a>
          </div>
        </div>
        <div class="footer-brand">
          <img src="@/assets/img/footers.png" alt="logo" class="footer-image" />
          <p>©2024 基因重组</p>
        </div>
      </div>
      <svg style="position:fixed; top:10vh">
        <defs>
          <filter id="blob">
            <feGaussianBlur in="SourceGraphic" stdDeviation="10" result="blur"/>
            <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 19 -9" result="blob"/>
          </filter>
        </defs>
      </svg>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onBeforeUnmount } from 'vue'
import $axios from '../components/utils/axios'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

const toLogin = () => {
  router.push("/login")
}

const formSize = ref('default')
const ruleFormRef = ref()
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入确认密码！'))
  } else if (value !== form.value.password) {
    callback(new Error("确认密码与密码不匹配！"))
  } else {
    callback()
  }
}
const rules = reactive({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    },
    {
      min: 5,
      max: 10,
      message: '5-10个数字或字母组合',
      trigger: 'blur'
    }
  ],
  name: [
    {
      required: true,
      message: '请输入真实姓名',
      trigger: 'blur'
    }
  ],
  telephone: [
    {
      required: true,
      message: '请输入手机号',
      trigger: 'blur'
    },
    {
      pattern: /^1[3456789]\d{9}$/,
      message: '手机号码格式不正确',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    },
    {
      min: 5,
      max: 10,
      message: '5-10个字符',
      trigger: 'blur'
    }
  ],
  checkPass: [
    { validator: validatePass2, required: true, trigger: 'blur' }
  ]
})

const form = ref({})

const countdown = ref(5)
let timer;
const onSubmit = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const path = '/user/register'
      const response = await $axios.put(path, form.value);
      const res = response.data;

      if (res.code === 200) {
        countdown.value = 5;
        timer = setInterval(() => {
          if (countdown.value > 1) {
            countdown.value--;
          } else {
            clearInterval(timer);
            router.push('/login');
          }
        }, 1000);
        ElMessage({
          showClose: true,
          message: `注册成功，${countdown.value}秒后跳转到登录页面。`,
          type: 'success',
        })

      } else if (res.code === 101) {
        ElMessage({
          showClose: true,
          message: '注册失败！',
          type: 'error',
        })
      } else if (res.code === 104) {
        ElMessage({
          showClose: true,
          message: '用户名已被注册！',
          type: 'error',
        })
      } else if (res.code === 105) {
        ElMessage({
          showClose: true,
          message: '手机号已被注册!',
          type: 'error',
        })
      } else if (res.code === 106) {
        ElMessage({
          showClose: true,
          message: '头像上传失败!',
          type: 'error',
        })
      }

    } else {
      console.log('error submit!', fields)
    }
  })
}

onBeforeUnmount(() => {
  clearInterval(timer);
});

const resetForm = (formEl) => {
  formEl.resetFields()
  if (!formEl) return
  formEl.resetFields()
}

const getBubbleStyle = () => {
  return {
    '--size': `${2 + Math.random() * 4}rem`,
    '--distance': `${6 + Math.random() * 4}rem`,
    '--position': `${-5 + Math.random() * 100}%`,
    '--time': `${2 + Math.random() * 2}s`,
    '--delay': `${-1 * (2 + Math.random() * 2)}s`
  }
}
</script>

<style lang="scss" scoped>
#container {
  display: flex;
  width: 100%;
  height: 100vh;
  flex-direction: column;
  background: url('@/assets/img/bg.jpeg') no-repeat center center fixed; /* 修改为背景图片 */
  background-size: cover; /* 让背景图片覆盖整个容器 */

  #header {
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;

    .title {
      font-size: 40px;
      color: #f8f8f8;
      font-family: 'Jost', sans-serif;
    }

    
  }

  #body {
    position: relative;
    flex: 2;
    margin-top: -50px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;

    .box-card {
      width: 500px; 
      height: 380px;
      overflow: hidden;
      border: 1px solid #64679e;
      border-radius: 20px; /* 增加圆角 */
      background: rgba(240, 248, 255, 0.85); /* Added transparency to show video background */
      font-size: 14px;
      box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); /* 添加阴影 */
   
      align-items: center;

      .card-header {
        margin-bottom: -40px;
        display: flex;
        justify-content: center;
        align-items: center;

        .info {
          font-size: 24px;
          color: #4a94d0;
            font-family: 'Raleway', sans-serif; /* 更改字体 */
        }
      }

      .registerwin {
        padding: 20px;
        border-radius: 20px; /* 增加圆角 */

        .registerform {
          .input-animated {
            width: 250px;
            transition: all 0.3s ease;
            &:focus {
              transform: scale(1.05);
              box-shadow: 0 0 10px rgba(0, 123, 255, 0.5);
            }
          }
          .btn-container {
            display: flex;
            justify-content: space-between;
            .button-animated {
              transition: all 0.3s ease;
              &:hover {
                transform: translateY(-2px);
                box-shadow: 0 4px 8px rgba(0, 123, 255, 0.3);
              }
            }
          }
        }
      }
    }
  }

  .footer {
    z-index: 1;
    --footer-background: linear-gradient(to right,#94ceee, #7cc6d5); /* 半透明水色渐变 */
    display: grid;
    position: relative;
    grid-area: footer;
    min-height: 4rem;
    .bubbles {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 1rem;
      background: var(--footer-background);
      filter: url("#blob");
      .bubble {
        position: absolute;
        left: var(--position, 50%);
        background: var(--footer-background);
        border-radius: 100%;
        animation: bubble-size var(--time, 4s) ease-in infinite var(--delay, 0s),
                   bubble-move var(--time, 4s) ease-in infinite var(--delay, 0s);
        transform: translate(-15%, 100%);
      }
    }
    .content {
      z-index: 2;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-gap: 1rem;
      padding: 1rem;
      font-size: 14px;
      background: var(--footer-background);
      a, p {
        color: #F5F7FA; 
        text-decoration: none;
        letter-spacing: 0.1rem;
      }
      b {
        color: white;
        letter-spacing: 0.1rem;
      }
      p {
        margin: 0;
        font-size: 14px;
      }
      > div {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin: auto;
        > div {
          margin: 0.25rem 0;
          > * {
            margin-right: .5rem;
          }
        }
        .image {
          align-self: center;
          width: 4rem;
          height: 4rem;
          margin: 0.25rem 0;
          background-size: cover;
          background-position: center;
        }
      }
      .footer-image {
        width: 80px;
        height: auto;
        display: block;
        margin:  auto;
      }
    }
    .footer-brand {
      display: flex;
      flex-direction: column;
      align-items: center;
      p {
        margin-top: 0.5rem;
      }
    }
  }
}

@keyframes bubble-size {
  0%, 75% {
    width: var(--size, 2rem);
    height: var(--size, 2rem);
  }
  100% {
    width: 0rem;
    height: 0rem;
  }
}
@keyframes bubble-move {
  0% {
    bottom: -1rem;
  }
  100% {
    bottom: var(--distance, 8rem);
  }
}

</style>
